import requests


def fetch_access_token(CROPID, SECERT):
    return 'TAJfxP6lXwPbYbKts0uz7UrEqwSCCt2L32dEf1wuV0aHWT82wQuBHZ3AkaIzWEWT6uN8SfQyC9-Gd75iakfQrF4GKl3S4qT62hj3-v5f21cXpXlygXS6CvgF33-FEmaF9rsL4Df5aaNurvCXLnMPQpckIrEldXvMiqgMxQSyiX6R8F-5xIBsVJiOTlv_WL3TN9zh28Ljan_MtMbWtEu-QA'
    """从微信服务器获取 access_token"""
    url = f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CROPID}&corpsecret={SECERT}'
    res = requests.get(url)
    access_token = res.json().get('access_token')
    if not access_token:
        return None
    return access_token


def fetch_en_jsapi_ticket(CROPID, SECERT):
    """从微信服务器获取企业jsapi_ticket"""
    access_token = fetch_access_token(CROPID, SECERT)
    if not access_token:
        return None
    url = f'https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token={access_token}'
    res = requests.get(url)
    ticket = res.json().get('ticket')
    if not ticket:
        return None
    return ticket


def fetch_app_jsapi_ticket(CROPID, SECERT):
    """从微信服务器获取应用jsapi_ticket"""
    access_token = fetch_access_token(CROPID, SECERT)
    if not access_token:
        return None
    url = f'https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token={access_token}&type=agent_config'
    res = requests.get(url)
    ticket = res.json().get('ticket')
    if not ticket:
        return None
    return ticket
